package com.situ.crm.controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.situ.crm.common.ServerResponse;
import com.situ.crm.entity.CusDevPlan;
import com.situ.crm.service.ICusDevPlanService;
import com.situ.crm.service.ISaleChanceService;

@Controller
@RequestMapping(value="/kaifa")
public class KaifaController {
	@Autowired
	private ICusDevPlanService cusDevPlanService;
	@Autowired
	private ISaleChanceService saleChanceService;
	
	//转发页面
	@RequestMapping(value="/getKaifaPage")
	private String getKaifaPage(Integer id,Model model) {
		model.addAttribute("id",id);
		System.err.println(id);
		return "kaifa/kaifa_list";
	}
	
	//根据sale_chance查出对应的计划
	@RequestMapping(value="/pageList")
	@ResponseBody
	public ServerResponse pageList(Integer limit,Integer page,Integer id) {
		System.err.println(id);
		ServerResponse serverResponse = cusDevPlanService.pageList(limit,page,id);
		return serverResponse;
	}
	
	//添加页面
	@RequestMapping(value="/getAddPage")
	public String getAddPage(Integer id,Model model) {
		model.addAttribute("id",id);
		return "kaifa/kaifa_insert";
	}
	
	//添加功能,同时把未开发设置为开发中
	@RequestMapping(value="/insert")
	@ResponseBody
	public ServerResponse insert(CusDevPlan cusDevPlan,Integer saleChanceId) {
		saleChanceService.updateDevResult(saleChanceId);
		return cusDevPlanService.insert(cusDevPlan);
	}
	
	//删除功能
	@RequestMapping(value="/deleteById")
	@ResponseBody
	public ServerResponse deleteById(Integer id) {
		return cusDevPlanService.deleteById(id);
	}
	
	//批量删除
	@RequestMapping(value="/deleteAll")
	@ResponseBody
	public ServerResponse deleteAll(String ids) {
		return cusDevPlanService.deleteAll(ids);
	}
	
	//修改页面
	@RequestMapping(value="/getUpdatePage")
	public String getUpdatePage(Integer id,Model model) throws ParseException {
		CusDevPlan cusDevPlan = cusDevPlanService.selectById(id);
		
		//页面展示日期格式
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		String time = dateFormat.format(cusDevPlan.getPlanDate());
		model.addAttribute("time",time);
		
		model.addAttribute("cusDevPlan",cusDevPlan);
		return "kaifa/kaifa_update";
	}
	
	//修改
	@RequestMapping(value="/update")
	@ResponseBody
	public ServerResponse update(CusDevPlan cusDevPlan) {
		return cusDevPlanService.updata(cusDevPlan);
	}
}
